package com.banzhi.shop.activity;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.banzhi.shop.R;
import com.banzhi.shop.adapter.CommonHeadAdapter;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.data.MyPartnerBean;

import com.banzhi.shop.data.MyPartnerData;
import com.banzhi.shop.model.CallBack.MyPartnerCallBack;
import com.banzhi.shop.model.Impl.MyPartnerImpl;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.EndlessRecyclerOnScrollListener;
import com.banzhi.shop.utils.LoadingDialogUtils;

import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;
import com.bumptech.glide.Glide;
import com.github.library.BaseRecyclerAdapter;


import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.banzhi.shop.R.id.srlayout;

//搜寻合伙人
public class FindPartnerActivity extends BaseActivity implements MyPartnerCallBack, SwipeRefreshLayout.OnRefreshListener {

    private MyPartnerImpl model;
    /**
     * 下拉刷新
     */
    private SwipeRefreshLayout srllistview;
    private int page = 1;
    private RelativeLayout rlnodata;
    private EditText etfind;
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;

  //  private MyPartnerBean mMyPartnerBean;
    List<MyPartnerData.DataBean.CitypartnerBean> CityPartners = new ArrayList<>();

    private BaseRecyclerAdapter<MyPartnerBean.CitypartnerBean> mRecyclerAdapter;
    private CommonHeadAdapter<MyPartnerData.DataBean.CitypartnerBean> adapter;
    private MyPartnerData mMyPartnerData;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_partner);
        Translucent.setTranslucent(this);
        setinits();
        setListeners();
        model = new MyPartnerImpl();
        LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
        model.getMyPartnerData(page + "", this);



    }

    private void setinits() {
        srllistview = (SwipeRefreshLayout) findViewById(srlayout);
        srllistview.setColorSchemeResources(R.color.themeColor);
        srllistview.setProgressBackgroundColorSchemeResource(R.color.colorWhite);
        rlnodata = (RelativeLayout) findViewById(R.id.rl_nodata);
        etfind = (EditText) findViewById(R.id.et_find);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
    }

    private void setListeners() {
        srllistview.setOnRefreshListener(this);
        etfind.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    page = 1;
                    model.getFindMyPartnerData(page + "", etfind.getText().toString().trim(), FindPartnerActivity.this);
                } else {
                    model.getMyPartnerData(page + "", FindPartnerActivity.this);

                }
            }
        });
        recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(layoutManager) {
            @Override
            public void onLoadMore() {
                page++;
                if (etfind.getText().length() == 0) {
                    LoadingDialogUtils.showProgress(FindPartnerActivity.this, "正在加载,请稍后~");
                    model.getMyPartnerData(page + "", FindPartnerActivity.this);
                } else {
                    LoadingDialogUtils.showProgress(FindPartnerActivity.this, "正在加载,请稍后~");
                    model.getFindMyPartnerData(page + "", etfind.getText().toString().trim(), FindPartnerActivity.this);
                }

            }
        });
    }

    @Override
    public void ongetMyPartnerDataSuccess(Object object) {
        LogUtils.d("MyPartnerDataSuccess", object.toString());
        if (srllistview.isRefreshing()) {
            srllistview.setRefreshing(false);
        }

        mMyPartnerData = (MyPartnerData) object;
        List<MyPartnerData.DataBean.CitypartnerBean> citypartner = mMyPartnerData.getData().getCitypartner();

        if (page == 1) {
            CityPartners.clear();
        } else {
            if ( citypartner.size() == 0){
                return;
            }
        }
        CityPartners.clear();
        CityPartners.addAll(citypartner);
        if (CityPartners.size() == 0) {
            rlnodata.setVisibility(View.VISIBLE);
        } else {
            rlnodata.setVisibility(View.GONE);
        }
        showView();
    }

    private void showView() {
        if (adapter == null) {
            adapter = new CommonHeadAdapter<MyPartnerData.DataBean.CitypartnerBean>(this, R.layout.recyclerview_item_partners, CityPartners) {
                @Override
                public void convert(ViewHolder holder, MyPartnerData.DataBean.CitypartnerBean bean, int position) {
                    holder.setText(R.id.tv_nickname,bean.getNickname());
                    holder.setText(R.id.tv_time,bean.getCitypartnertime());
                    if (ASUtils.isCanUse(bean.getAvatar())) {
                        Glide.with(mContext).load(bean.getAvatar()).placeholder(R.mipmap.zhanweitu2).into((ImageView) holder.getView(R.id.civ));
                    }

                }

                @Override
                public void headconvert(HeaderViewHolder holder) {

                }
            };
            recyclerView.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }


    }

    @Override
    public void onRefresh() {
        page = 1;
        if (etfind.getText().length() == 0) {
            LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
            model.getMyPartnerData(page + "", FindPartnerActivity.this);

        } else {
            LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
            model.getFindMyPartnerData(page + "", etfind.getText().toString().trim(), FindPartnerActivity.this);
        }
    }

    @Override
    public void ongetMyPartnerDataError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));


    }
}


